<?php

include_once "conf.php";
include_once "rrdwrapper.php";

$jobsdir=$rrds."/mrjob";

// "mrjob/jobid/host/metric.rrd"
function getJobSummary($jobid, $hostlist, $metname, $prefix) {
  global $jobsdir, $metric_scale;
  $defs = "";
  $sum = NULL;
  $cnt = 0;
  foreach ($hostlist as $host) {
    $cnt += 1;
    $fname = "$jobsdir/$jobid/$host/gauge-$prefix"."_$metname.rrd";
    if (file_exists($fname)) {
      if ($prefix != NULL) {
        $var = new RRDDef("$prefix$cnt", $fname, "value", "AVERAGE");
      } else {
        $var = new RRDDef("a$cnt", $fname, "value", "AVERAGE");
      }
      $defs.=$var->showDef()." ";
      if ($sum == NULL) {
        $sum = new RRDCDef("", "", $var, NULL);
      } else {
        $sum = $sum->add($var);
      }
    }
  }
  if ($sum != NULL && array_key_exists($metname, $metric_scale)) {
    $sum = $sum->mul(new RRDConst($metric_scale[$metname]));
  }
  if ($sum != NULL) {
    $sum->vname = "$jobid"."_$prefix"."_$metname";
    $defs.=$sum->showDef();
  } else {
    $defs = NULL;
  }
  return $defs;
}

// "host/process/metric.rrd"
function getProcessSummary($proc, $hostlist, $metname) {
  global $rrds, $metric_scale;
  $defs = "";
  $sum = NULL;
  foreach ($hostlist as $host) {
    $fname = "$rrds/$host/$proc/gauge-$metname.rrd";
    if (file_exists($fname)) {
      $var = new RRDDef("$proc"."_"."$host"."_"."$metname", $fname, "value", "AVERAGE");
      $defs.=$var->showDef()." ";
      if ($sum == NULL) {
        $sum = new RRDCDef("", "", $var, NULL);
      } else {
        $sum = $sum->add($var);
      }
    }
  }
  if ($sum != NULL && array_key_exists($metname, $metric_scale)) {
    $sum = $sum->mul(new RRDConst($metric_scale[$metname]));
  }
  if ($sum != NULL) {
    $sum->vname = "$proc"."_"."$metname";
    $defs.=$sum->showDef();
  } else {
    $defs = NULL;
  }
  return $defs;
}

// "host/plugin-plugin_instance/type-type_instance"
function getCollectdMetric($var, $host, $plugin, $plugin_instance, $type, $type_instance) {
  global $rrds;
  $fname = "$rrds/$host/$plugin-$plugin_instance/$type-$type_instance";
  $def = new RRDDef($var, $fname);
  return $def->showDef();
}

function getJobClusterElems($jobid, $metname) {
  global $unique_color, $hadoopProcList;
  $elems = "";
  $cnt = 0;
  $elems .= "LINE2:'$jobid"."_"."$metname'#$unique_color[$cnt]:'$jobid' ";
  foreach ($hadoopProcList as $proc) {
    $cnt += 1;
    $elems .= "LINE2:'$proc"."_"."$metname'#$unique_color[$cnt]:'$proc' ";
  }
  return $elems;
}

function getJobClusterView($jobid, $metname, $hostlist) {
  global $jobsdir, $hadoopProcList;
  $elem = array();
  $lable = array();
  $def = getJobSummary($jobid, $hostlist, $metname, NULL);
  if ($def != NULL) {
    $elem[] = "$jobid"."_"."$metname";
    $lable[] = "$jobid";
  }
  foreach ($hadoopProcList as $proc) {
    $def .= " ".getProcessSummary($proc, $hostlist, $metname);
    if ($def != NULL) {
      $elem[] = "$proc"."_"."$metname";
      $lable[] = "$proc";
    }
  }
  return array($def, $elem, $lable);
}

function getJobNodeElems($jobid, $metname) {
  global $unique_color, $hadoopProcList;
  $elems = "";
  $cnt = 0;
  $elems .= "LINE2:'$jobid"."_"."$metname'#$unique_color[$cnt]:'$jobid' ";
  foreach ($hadoopProcList as $proc) {
    $cnt += 1;
    $elems .= "LINE2:'$proc"."_"."$metname'#$unique_color[$cnt]:'$proc' ";
  }
  return $elems;
}

function getJobNodeView($jobid, $host, $metname) {
  global $hadoopProcList;
  $def = getJobSummary($jobid, array($host), $metname, NULL);
  $elem = array();
  $lable = array();
  if ($def != NULL) {
    $elem[] = "$jobid"."_"."$metname";
    $lable[] = "$jobid";
  }
  foreach ($hadoopProcList as $proc) {
    $def .= " ".getProcessSummary($proc, array($host), $metname);
  }
  return $def;
}

function getJobNodeViewNew($jobid, $host, $metname) {
  global $hadoopProcList;
  global $numMapSlots;
  global $numReduceSlots;
  $def = "";
  $elem = array();
  $lable = array();
  for ($i = 0; $i < $numMapSlots; $i++) {
    $slotDef = getJobSummary($jobid, array($host), $metname, "m$i");
    if ($slotDef != NULL) {
      $def .= " ".$slotDef;
      $elem[] = "$jobid"."_m$i"."_$metname";
      $lable[] = "$jobid"."_m$i";
    }
  }
  for ($i = 0; $i < $numReduceSlots; $i++) {
    $slotDef = getJobSummary($jobid, array($host), $metname, "r$i");
    if ($slotDef != NULL) {
      $def .= " ".$slotDef;
      $elem[] = "$jobid"."r$i"."_$metname";
      $lable[] = "$jobid"."_r$i";
    }
  }
  foreach ($hadoopProcList as $proc) {
    $procDef = getProcessSummary($proc, array($host), $metname);
    if ($procDef != NULL) {
      $def .= " ".$procDef;
      $elem[] = "$proc"."_"."$metname";
      $lable[] = "$proc";
    }
  }
  return array($def, $elem, $lable);
}

function getElems($vnames, $labels) {
  global $unique_color;
  $elems = "";
  $cnt = 0;
  foreach ($vnames as $vname) {
    $label = $labels[$cnt];
    if ($label == "hadoop-Total") {
      $label = "Total";
    }
    $elems .= "LINE5:'$vname'#$unique_color[$cnt]:'$label' ";
    $cnt += 1;
  }
  return $elems;
}

?>
